package com.jstyle.jclife.view;

import android.content.Context;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;
import android.view.View;

import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.ExerciseRecodeData;
import com.haibin.calendarview.WeekView;
import com.jstyle.jclife.R;

/**
 * 多彩周视图
 * Created by huanghaibin on 2017/11/29.
 */

public class ExerciseWeekView extends WeekView {
    int colorStep = Color.parseColor("#264cef");
    int colorExercise = Color.parseColor("#e9603c");
    int colorSleep = Color.parseColor("#607ff4");
    int colorHeart = Color.parseColor("#d3052c");
    private int mRadius;
    private Paint mRingPaint = new Paint();
    private int mRingRadius;

    public ExerciseWeekView(Context context) {
        super(context);
        //兼容硬件加速无效的代码
        setLayerType(View.LAYER_TYPE_SOFTWARE, mSelectedPaint);
        //4.0以上硬件加速会导致无效
        mSelectedPaint.setMaskFilter(new BlurMaskFilter(30, BlurMaskFilter.Blur.SOLID));
        colorStep=getResources().getColor(R.color.exercise_step);
        colorExercise=getResources().getColor(R.color.exercise_sport);
        colorSleep=getResources().getColor(R.color.exercise_sleep);
        colorHeart=getResources().getColor(R.color.exercise_heart);
        setLayerType(View.LAYER_TYPE_SOFTWARE, mSchemePaint);
        mSchemePaint.setStyle(Paint.Style.STROKE);
        mSchemePaint.setStrokeWidth(dipToPx(context, 1));
       // mSchemePaint.setColor(context.getResources().getColor(R.color.exercise_month_bg));
        mRingPaint.setAntiAlias(true);
        mRingPaint.setColor(context.getResources().getColor(R.color.exercise_month_bg));

        mRingPaint.setStyle(Paint.Style.FILL);
        mRingPaint.setStrokeWidth(dipToPx(context, 1));
        setLayerType(View.LAYER_TYPE_SOFTWARE, mRingPaint);
     //   mRingPaint.setMaskFilter(new BlurMaskFilter(30, BlurMaskFilter.Blur.SOLID));
    }

    @Override
    protected void onPreviewHook() {
        mRadius = Math.min(mItemWidth, mItemHeight) / 6 * 2;
        mRingRadius = Math.min(mItemWidth, mItemHeight) / 5 * 2;
        mSelectTextPaint.setTextSize(dipToPx(getContext(),17));
    }

    /**
     * 如果需要点击Scheme没有效果，则return true
     *
     * @param canvas    canvas
     * @param calendar  日历日历calendar
     * @param x         日历Card x起点坐标
     * @param hasScheme hasScheme 非标记的日期
     * @return false 则不绘制onDrawScheme，因为这里背景色是互斥的
     */
    @Override
    protected boolean onDrawSelected(Canvas canvas, Calendar calendar, int x, boolean hasScheme) {
//        int cx = x + mItemWidth / 2;
//        int cy = mItemHeight / 2;
//        canvas.drawCircle(cx, cy, mRadius, mSelectedPaint);
//        canvas.drawCircle(cx, cy, mRingRadius, mRingPaint);
        return true;
    }


    @Override
    protected void onDrawScheme(Canvas canvas, Calendar calendar, int x) {
//        int cx = x + mItemWidth / 2;
//        int cy =  mItemHeight / 2;
//
//
//        Rect rect=new Rect();
//        rect.left=cx-mItemWidth/3;
//        rect.right=cx+mItemWidth/3;
//        rect.top=cy-mItemHeight/3;
//        rect.bottom=cy+mItemHeight/3;
//        canvas.drawRect(rect,mSchemePaint);
    }

    private static final String TAG = "ExerciseWeekView";

    @Override
    protected void onDrawText(Canvas canvas, Calendar calendar, int x, boolean hasScheme, boolean isSelected) {

        float baselineY = mTextBaseLine - dipToPx(getContext(), 1);
        int cx = x + mItemWidth / 2;
        int cy =  mItemHeight / 2;
        Rect rectText = new Rect();
        mCurMonthTextPaint.getTextBounds(String.valueOf(calendar.getDay()), 0, String.valueOf(calendar.getDay()).length(), rectText);
        int height = rectText.height();
        int width=rectText.width()/2;
        int margin=dipToPx(getContext(),5);
        int rectWidth=mItemWidth-margin;
        int rectHeight=mItemHeight-margin;
        Rect rect=new Rect();
        rect.left=cx-rectWidth/2;
        rect.right=cx+rectWidth/2;
        rect.top=cy-rectHeight/2;
        rect.bottom=cy+rectHeight/2;
        canvas.drawRect(rect,mRingPaint);
         if (hasScheme) {
            canvas.drawText( String.valueOf(calendar.getDay()),
                    width+margin,
                    height+margin,
                            calendar.isCurrentMonth() ? mSchemeTextPaint : mOtherMonthTextPaint);

        } else {
            canvas.drawText( String.valueOf(calendar.getDay()),
                    x+width+margin,
                    height+margin,
                            calendar.isCurrentMonth() ? mCurMonthTextPaint : mOtherMonthTextPaint);
        }
        String date=calendar.getYear()+"."+String.format("%02d",calendar.getMonth())+"."+String.format("%02d",calendar.getDay());

        Log.i(TAG, "onDrawText: "+date);
        ExerciseRecodeData exerciseRecodeData=maps.get(date);
        if (exerciseRecodeData!=null) {
            mSchemePaint.setStyle(Paint.Style.FILL);
            int y=0;
            if(exerciseRecodeData.getStepStatus()!=ExerciseRecodeData.NoData) drawStepData(canvas, x, y,exerciseRecodeData.getStepStatus());
            if(exerciseRecodeData.getExercise()==ExerciseRecodeData.HasData)drawSportData(canvas, x, y);
            if(exerciseRecodeData.getSleepStatus()!=ExerciseRecodeData.NoData) drawSleepData(canvas, x, y,exerciseRecodeData.getSleepStatus());
            if(exerciseRecodeData.getHeartStatus()==ExerciseRecodeData.HasData)drawHeartData(canvas, x, y);
        }
    }

    private int offsetValue = 8;
    private int margin = 5;
    private int widthValue = 5;

    private void drawStepData(Canvas canvas, int x, int y,int data) {
        mSchemePaint.setColor(colorStep);
        Log.i(TAG, "drawStepData: ");
        int offset = dipToPx(getContext(), offsetValue);
        int offsetTop = dipToPx(getContext(), 22);
        int offsetBottom = dipToPx(getContext(), 8);
        int width = dipToPx(getContext(), widthValue);
        int height=mItemHeight - offsetBottom-offsetTop;
        if(data==ExerciseRecodeData.HasData){
            canvas.drawRect(x + offset, y + offsetTop, x + offset + width, y + mItemHeight - offsetBottom, mSchemePaint);
        }else{
            canvas.drawRect(x + offset, y+mItemHeight - offsetBottom-height/2, x + offset + width, y + mItemHeight - offsetBottom, mSchemePaint);
        }
    }

    private void drawSportData(Canvas canvas, int x, int y) {
        mSchemePaint.setColor(colorExercise);
        Log.i(TAG, "drawSportData: ");
        int offset = dipToPx(getContext(), offsetValue + widthValue + margin);
        int offsetTop = dipToPx(getContext(), 22);
        int offsetBottom = dipToPx(getContext(), 8);
        int width = dipToPx(getContext(), widthValue);
        canvas.drawRect(x + offset, y + offsetTop, x + offset + width, y + mItemHeight - offsetBottom, mSchemePaint);
    }

    private void drawSleepData(Canvas canvas, int x, int y,int data) {
        mSchemePaint.setColor(colorSleep);
        Log.i(TAG, "drawSleepData: ");
        int offset = dipToPx(getContext(), offsetValue + (widthValue + margin) * 2);
        int offsetTop = dipToPx(getContext(), 22);
        int offsetBottom = dipToPx(getContext(), 8);
        int width = dipToPx(getContext(), widthValue);
        int height=mItemHeight - offsetBottom-offsetTop;
        if(data==ExerciseRecodeData.HasData){
            canvas.drawRect(x + offset, y + offsetTop, x + offset + width, y + mItemHeight - offsetBottom, mSchemePaint);
        }else{
            canvas.drawRect(x + offset, y+mItemHeight - offsetBottom-height/2, x + offset + width, y + mItemHeight - offsetBottom, mSchemePaint);
        }
    }

    private void drawHeartData(Canvas canvas, int x, int y) {
        mSchemePaint.setColor(colorHeart);
        Log.i(TAG, "drawHeartData: ");
        int offset = dipToPx(getContext(), offsetValue + (widthValue + margin) * 3);
        int offsetTop = dipToPx(getContext(), 22);
        int offsetBottom = dipToPx(getContext(), 8);
        int width = dipToPx(getContext(), widthValue);
        canvas.drawRect(x + offset, y + offsetTop, x + offset + width, y + mItemHeight - offsetBottom, mSchemePaint);
    }
    /**
     * dp转px
     *
     * @param context context
     * @param dpValue dp
     * @return px
     */
    @SuppressWarnings("all")
    private static int dipToPx(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
